'use strict';

Object.defineProperty(exports, "__esModule", {
    value: true
});

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); /**
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      * 大拇指点赞功能
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      */


var _myMath = require('./myMath.js');

var _myPromise = require('./myPromise.js');

var _myAxios = require('./myAxios.js');

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var ThumbUp = function () {
    function ThumbUp(ele, numEle) {
        _classCallCheck(this, ThumbUp);

        this.ele = ele;
        this.numEle = numEle;
        this.currNum = 0;
        this.visitor = 'visitor' + Math.floor(Math.random() * 100);
    }

    _createClass(ThumbUp, [{
        key: 'clickEvent',
        value: function clickEvent() {
            this.ele.click(this.clickThump());
        }
    }, {
        key: 'removeAnimation',
        value: function removeAnimation() {
            this.ele.removeClass(this.getScale());
            this.numEle.removeClass(this.getAddOne());
        }
    }, {
        key: 'setAnimation',
        value: function setAnimation() {
            this.ele.addClass(this.getScale());
            this.numEle.addClass(this.getAddOne());
        }
    }, {
        key: 'saveThumbUpNum',
        value: function saveThumbUpNum() {
            var _this = this;

            // method1：使用Promise，依赖$.ajax
            /*myPromise({
                ajax: $.ajax,
                url: 'app/mysql/thumbupOob.php',
                params: {
                    num: this.currNum,
                    creator: this.visitor
                }
            }).then(() => {
                console.log('第 ${this.currNum} 次点赞成功！');
            }, (error) => {
                console.error('第 ${this.currNum} 次点赞失败：${error}');
                this.removeAnimation();
                this.currNum = this.getDefaultNum();
            });*/
            // method2：使用axios，不依赖其他组件，很纯
            (0, _myAxios.myAxios)().get('app/mysql/thumbupOob.php', {
                params: {
                    num: this.currNum,
                    creator: this.visitor
                }
            }).then(function () {
                console.log('\u7B2C ' + _this.currNum + ' \u6B21\u70B9\u8D5E\u6210\u529F\uFF01');
            }).catch(function (error) {
                console.error('\u7B2C ' + _this.currNum + ' \u6B21\u70B9\u8D5E\u5931\u8D25\uFF1A' + error);
                _this.removeAnimation();
                _this.currNum = _this.getDefaultNum();
            });
            // post方式不行
            /*myAxios({
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
            }).post('app/mysql/thumbupOob.php', {}, {
                data: {
                    num: this.currNum,
                    creator: this.visitor
                },
            }).then(() => {
                console.log(`第 ${this.currNum} 次点赞成功！`);
            }).catch((error) => {
                console.error(`第 ${this.currNum} 次点赞失败：${error}`);
                this.removeAnimation();
                this.currNum = this.getDefaultNum();
            });*/
        }
    }, {
        key: 'getScale',
        value: function getScale() {
            return 'thumbScale';
        }
    }, {
        key: 'getAddOne',
        value: function getAddOne() {
            return 'addOne';
        }
    }, {
        key: 'getDefaultNum',
        value: function getDefaultNum() {
            return 0;
        }
    }, {
        key: 'getInterval',
        value: function getInterval() {
            return 1000;
        }
    }]);

    return ThumbUp;
}();

var MyThumb = function (_ThumbUp) {
    _inherits(MyThumb, _ThumbUp);

    function MyThumb(ele, numEle, currNum) {
        _classCallCheck(this, MyThumb);

        return _possibleConstructorReturn(this, (MyThumb.__proto__ || Object.getPrototypeOf(MyThumb)).call(this, ele, numEle, currNum));
    }

    _createClass(MyThumb, [{
        key: 'clickThump',
        value: function clickThump() {
            var _this3 = this;

            var lastClickTime = void 0;
            return function () {
                var currTime = new Date().getTime();
                // 本次点击时间在周期内，不提交
                if (lastClickTime && currTime - lastClickTime < _this3.getInterval()) {
                    alert('亲，点赞过于频繁啊，稍后点赞吧！');
                    return;
                }
                lastClickTime = currTime;
                // 将大拇指放大，旁边显示+1
                _this3.setAnimation();
                _this3.currNum = (0, _myMath.count)(_this3.currNum);
                // 保存本次点赞
                _this3.saveThumbUpNum();
                setTimeout(function () {
                    _this3.removeAnimation();
                }, _this3.getInterval());
            };
        }
    }]);

    return MyThumb;
}(ThumbUp);

exports.default = {
    MyThumb: MyThumb
};